package com.irun.sm.ui.demo.view;

import java.util.Calendar;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.text.TextPaint;
import android.view.View;
import android.view.ViewGroup;

/***
 * @author huangsm
 * @date 2012-6-20
 * @email huangsanm@gmail.com
 * @desc 时间控件
 */
public class DateTimePickerView extends View {

	private final static String[] mWEEKS = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
	private Context mContext;
	
	private Paint mPaintWeek;
	private Paint mPaintDate;
	private int mWidth;
	private int mHeight;
	private float mWeekPadding;
	
	public DateTimePickerView(Context context, int width, int height) {
		super(context);
		mContext = context;
		mWidth = width;
		mHeight = height;
		
		System.out.println("w:" + mWidth + "," + mHeight);
		
		mWeekPadding = mWidth / mWEEKS.length;
		
		mPaintWeek = new Paint();
		mPaintWeek.setColor(Color.WHITE);
		mPaintWeek.setAntiAlias(true);
		mPaintWeek.setFakeBoldText(true);
		mPaintWeek.setTextSize(17);
		
	}
	
	@Override
	protected void onDraw(Canvas canvas) {
		//super.onDraw(canvas);
		//绘制星期
		int week_left = 10;
		for (int i = 0; i < mWEEKS.length; i++) {
			canvas.drawText(mWEEKS[i], week_left, 40, mPaintWeek);
			week_left += mWeekPadding;
		}
		
		//绘制日期
		week_left = 10;
		int height = 50;
		final int days = getMaxDayOfMonth();
		final int dayOfmonth = getDayOfMonth();
		System.out.println("dayOfMonth:" + dayOfmonth);
		System.out.println("days:" + days);
		for (int i = 1; i <= days; i++) {
			canvas.drawText(i + "", week_left, height + 30, mPaintWeek);
			if(i % 7 == 0){
				week_left = 10;
				height += 35;
			}else{
				week_left += mWeekPadding;
			}
		}
	}
	
	private int getMaxDayOfMonth(){
		Calendar cal = Calendar.getInstance();
		return cal.getActualMaximum(Calendar.DATE);
	}
	
	private int getDayOfMonth(){
		Calendar cal = Calendar.getInstance();
		return cal.get(Calendar.DAY_OF_WEEK);
	}
}
